const express = require('express')
const fileHandle  = require('../../src/utils/fileHandle')
const Unique = require('../../src/utils/Unique')
const createTime = require('../../src/utils/creatTime')
const router = express.Router()
// 获取店铺信息
router.get("/getMenuList",async (req,res,next)=>{
    const {id,type,search}=req.query

    let data=await fileHandle.read("../files/memuList")

    let result=data.find((item)=>{
        return item.shopId===id
    })
    //热销
    if(type==="hot"){
        result=result?result.kindMenus.reduce((pre,jtem)=>{
            jtem.items.forEach((j)=>{
                if(j.hot){
                    pre.push(j)
                }
            })
            return pre
        },[]):[]
    }

    //搜索
    if(search!==undefined){
        console.log(search)
        result=result?result.kindMenus.reduce((pre,jtem)=>{
            jtem.items.forEach((j)=>{
                if(j.name.includes(search)){
                    pre.push(j)
                }
            })
            return pre
        },[]):[]
    }

    res.send({
        code:200,
        data:result||{},
        msg:"ok"
    })
})
// 呼叫服务员
router.post('/callOut',async(req,res)=>{
    const {shopId,tableNum,userId} = req.query
    const result = {
        id:Unique(),
        shopId,
        tableNum,
        userId,
        createTime:createTime()
    }
    await fileHandle.add('../files/callOut',result)
    res.send({
        code:200,
        msg:"ok"
    })
})
module.exports = router